//
// Created by Administrator on 2022/3/26.
//

#include "config.h"
#include <fstream>
namespace TinyRPC{

Config::ConfigMap Config::configMap = ConfigMap();
void Config::parseLine(const string & line){
    //注释 返回
    if(startWith(line, '#')){
        return;
    }
    int endPos = line.find(line, '#');
    if (endPos != string::npos){
        string newLine = line.substr(0, endPos);

        vector<string> strs;
        splitString(newLine, strs, "=");
        if(strs.size() != 2){
            return;
        }
        configMap[trim(strs[0], ' ')] = trim(strs[1], ' ');
    }else{
        vector<string> strs;
        splitString(line, strs, "=");
        if(strs.size() != 2){
            return;
        }
        configMap[trim(strs[0], ' ')] = trim(strs[1], ' ');
    }

}
void Config::init(const string &filePath) {
    ifstream in(filePath);
    string line;
    if(in){
        while (getline(in, line)){
            if(line[line.size()-1] == '\r'){
                line[line.size()-1] = ' ';
            }

            parseLine(line);
        }
        in.close();
    }
}



}
